<html>
<head><meta charset="utf-8"><title>Is it UB to create uninitialized i32? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html">Is it UB to create uninitialized i32?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206339432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> moxian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339432">(Aug 08 2020 at 09:00)</a>:</h4>
<p>Quick question (and apologies if I'm using zullip threads wrong, first time dealing with these).<br>
Reference states (<a href="https://doc.rust-lang.org/reference/behavior-considered-undefined.html">https://doc.rust-lang.org/reference/behavior-considered-undefined.html</a>): </p>
<blockquote>
<p>The following list is just what we know for sure is undefined behavior:<br>
Producing an invalid value, even in private fields and locals. "Producing" a value happens any time a value is assigned to or read from a place, passed to a function/primitive operation or returned from a function/primitive operation. The following values are invalid (at their respective type): <br>
....<br>
An integer (i<em>/u</em>), floating point value (f*), or raw pointer obtained from uninitialized memory, or uninitialized memory in a str.</p>
</blockquote>
<p>This implies that the following code is UB (playground: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=31411573fc38214bd2e58b21361646a0">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=31411573fc38214bd2e58b21361646a0</a> ):</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">(){</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">uninitialized</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="nb">drop</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>yet miri is happy with it.</p>
<p>Is it a bug in miri, or in the reference?</p>
<p>(I understand that using such uninit value pretty much anywhere is UB, I'm asking whether merely "producing" ("assigning to a place") it is)</p>



<a name="206339669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339669">(Aug 08 2020 at 09:08)</a>:</h4>
<p>it is a known limitation in miri</p>



<a name="206339685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339685">(Aug 08 2020 at 09:09)</a>:</h4>
<p>as the miri README documents, Miri does not catch all UB. sometimes because it is hard, sometimes because I am worried it might flag too many errors.<br>
<span class="user-mention" data-user-id="120828">@moxian</span>  your code example is indeed UB but at <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/71">https://github.com/rust-lang/unsafe-code-guidelines/issues/71</a> we are discussing whether we should change that (though I am more and more leaning towards no, let's leave it UB)</p>



<a name="206339688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> moxian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339688">(Aug 08 2020 at 09:10)</a>:</h4>
<p>I see, thank you!</p>



<a name="206339727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339727">(Aug 08 2020 at 09:10)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/miri/issues/1340">https://github.com/rust-lang/miri/issues/1340</a>: I plan to add a flag to make miri find this UB</p>



<a name="206339728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339728">(Aug 08 2020 at 09:10)</a>:</h4>
<p>but haven't gotten around to implementing that yet</p>



<a name="206339731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339731">(Aug 08 2020 at 09:10)</a>:</h4>
<p>(also it would likely not be available on the playground, unless we can convince <span class="user-mention" data-user-id="116155">@Jake Goulding</span> to somehow let us pass arbitrary CLI flags there^^)</p>



<a name="206339948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206339948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> moxian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206339948">(Aug 08 2020 at 09:18)</a>:</h4>
<p>Oh, and one more thing. Since <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/71">https://github.com/rust-lang/unsafe-code-guidelines/issues/71</a> is still open, and uninit integers are not "really" UB right now, is it slightly incorrect that the reference claims "The following list is just what we know for sure is undefined behavior". As in, we do not know for sure that it's UB right now (but reference says that it is, out of abundance of caution, so that people don't rely on it and end in trouble when we decide that it is), right?</p>



<a name="206343003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206343003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206343003">(Aug 08 2020 at 10:54)</a>:</h4>
<p>I'm pretty sure uninit integers are UB right now, but <a href="https://github.com/rust-lang/rust/issues/71">#71</a> propose to change that</p>



<a name="206343604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206343604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206343604">(Aug 08 2020 at 11:13)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <a href="https://github.com/integer32llc/rust-playground/issues/446">https://github.com/integer32llc/rust-playground/issues/446</a></p>



<a name="206343974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206343974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206343974">(Aug 08 2020 at 11:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F/near/206343003">said</a>:</p>
<blockquote>
<p>I'm pretty sure uninit integers are UB right now, but <a href="https://github.com/rust-lang/rust/issues/71">#71</a> propose to change that</p>
</blockquote>
<p><span class="user-mention" data-user-id="120828">@moxian</span> this^</p>



<a name="206344015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206344015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206344015">(Aug 08 2020 at 11:26)</a>:</h4>
<p>Rust evolves, the reference describes the current version</p>



<a name="206344018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206344018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206344018">(Aug 08 2020 at 11:26)</a>:</h4>
<p>relaxing UB is forwards-compatible, but making more things UB is not</p>



<a name="206344019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20it%20UB%20to%20create%20uninitialized%20i32%3F/near/206344019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20it.20UB.20to.20create.20uninitialized.20i32.3F.html#206344019">(Aug 08 2020 at 11:26)</a>:</h4>
<p>so the "abundance of caution" is in making sure we are forward-compatible with either choice</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>